﻿using AutoMapper;
using B.S.Meeting.Domain;
using B.S.Meeting.ErrorCode;
using B.S.Meeting.Infrastructure.Interfaces;
using B.S.Meeting.WriteAPI.Applications.Commands.MeetingRooms;
using MediatR;

namespace B.S.Meeting.WriteAPI.Applications.CommandHandlers.MeetingRooms
{
    public class UpdRoomCommandHandler : IRequestHandler<UpdRoomCommand, APIResult<int>>
    {
        IBaseRepository<MeetingRoom> mrRep;
        ILogger<UpdRoomCommandHandler> logger;

        public UpdRoomCommandHandler(IBaseRepository<MeetingRoom> mrRep, ILogger<UpdRoomCommandHandler> logger)
        {
            this.mrRep = mrRep;
            this.logger = logger;
        }

        public async Task<APIResult<int>> Handle(UpdRoomCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var list = await mrRep.UpdAsync(request.Data);
                return new APIResult<int>
                {
                    Code=StatusEnumCode.Ok,
                    Message="修改会议室成功",
                    Data=list
                };
            }
            catch (Exception ex)
            {
                logger.LogError("修改会议室出错",ex.Message);
                throw;
            }
        }
    }
}
